package top.likeqc.util;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Component;
import top.likeqc.entity.SysUser;
import top.likeqc.service.SysRoleService;
import top.likeqc.service.SysUserService;

/**
 * @author likeqc
 * @date 2022年04月15日 9:34
 */
@Component
public class SecurityUtil {

    @Autowired
    SysUserService sysUserService;

    @Autowired
    SysRoleService sysRoleService;

    public SysUser getSysUser() {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        String name = authentication.getName();
        SysUser sysUser = sysUserService.getByUsername(name);
        sysUser.setSysRoles(sysRoleService.listRoleByUserId(sysUser.getId()));

        return sysUser;
    }
}
